package com.design.softwaredesign.service.impl;

import com.design.softwaredesign.entity.Donation;
import com.design.softwaredesign.entity.DonationLog;
import com.design.softwaredesign.entity.Good;
import com.design.softwaredesign.mapper.BackDonationMapper;
import com.design.softwaredesign.service.BackDonationService;
import com.design.softwaredesign.vo.DataVo;
import com.design.softwaredesign.vo.DonationVo;
import com.design.softwaredesign.vo.GoodVo;
import com.design.softwaredesign.vo.TimeStampVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/15 9:12
 */
@Service
public class BackDonationServiceImpl implements BackDonationService {
    @Autowired
    private BackDonationMapper backDonationMapper;
    @Override
    public DataVo getDonationNoArrival(Integer pageNum, Integer pageSize, String query) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("获取捐赠物品失败");
        PageHelper.startPage(pageNum, pageSize);
        List<Donation> donationList = backDonationMapper.getDonationNoArrival(query);
        PageInfo<Donation> donationPageInfo = new PageInfo<>(donationList);
        List<DonationVo> donationVoList = new ArrayList<>();
        Donation donation = null;
        for(int i = 0; i < donationList.size(); i++) {
            DonationVo donationVo = new DonationVo();
            donation = donationList.get(i);
            BeanUtils.copyProperties(donation, donationVo);
            if(!donation.getImageUrl().equals("")) {
                String []imageUrls = donation.getImageUrl().split(";");
                List<String> imageUrlList = new ArrayList<>();
                for(String imageUrl : imageUrls) {
                    imageUrlList.add(imageUrl);
                }
                donationVo.setImageUrlList(imageUrlList);
            }

            if(donation.getAcceptStaet() == 0) {
                donationVo.setDonationState("未处理");
            }
            if(donation.getAcceptStaet() == 1) {
                donationVo.setDonationState("已接收");
            }
            if(donation.getTransationState() == 1) {
                donationVo.setDonationState("运输中");
            }
            donationVoList.add(donationVo);
        }
        dataVo.setCode(200);
        dataVo.setMsg("获取捐赠物品成功");
        dataVo.setData(donationVoList);
        dataVo.setCount(donationPageInfo.getTotal());
        return dataVo;
    }

    @Override
    public DataVo changeDonationState(String donationId, Integer num) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("修改捐赠状态失败");
        try {
            SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化当前系统日期
            String nowTime = dateFm.format(new java.util.Date());
            if(num == 1) {
                backDonationMapper.changeAcceptState(nowTime, donationId);
            }
            if(num == 2) {
                backDonationMapper.changeTransationState(nowTime, donationId);
            }
            if(num == 3) {
                backDonationMapper.changeArrivalState(nowTime,donationId);
            }
            dataVo.setCode(200);
            dataVo.setMsg("修改捐赠状态成功");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getDonationTimestamp(String donationId) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("捐赠物品时间线获取成功");
        try {
            List<TimeStampVo> timeStampVoList = new ArrayList<>();
            DonationLog donationLog = backDonationMapper.getDonationTimestamp(donationId);
            if(donationLog.getPostState() == 1) {
                TimeStampVo timeStampVo1 = new TimeStampVo(donationLog.getPostTime(), "发布捐赠成功");
                timeStampVoList.add(timeStampVo1);
            }
            if(donationLog.getAcceptStaet() == 1) {
                TimeStampVo timeStampVo2 = new TimeStampVo(donationLog.getAccepTime(), "管理员接收捐赠成功");
                timeStampVoList.add(timeStampVo2);
            }
            if(donationLog.getTransationState() == 1) {
                TimeStampVo timeStampVo3 = new TimeStampVo(donationLog.getTransationTime(), "已向公益对象所在地发件");
                timeStampVoList.add(timeStampVo3);
            }
            if(donationLog.getArrivalState() == 1) {
                TimeStampVo timeStampVo4 = new TimeStampVo(donationLog.getArrivalTime(), "公益对象已成功收到您捐赠的物品");
                timeStampVoList.add(timeStampVo4);
            }
            dataVo.setCode(200);
            dataVo.setMsg("获取捐赠时间线成功");
            dataVo.setData(timeStampVoList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo deleteDonationById(String donationId) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("删除捐赠失败");
        try {
            if(backDonationMapper.deleteDonationById(donationId) == 1) {
                backDonationMapper.deleteDonationImages(donationId);
            }
            dataVo.setCode(200);
            dataVo.setMsg("删除捐赠成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getDonationArrival(Integer pageNum, Integer pageSize, String query) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("获取捐赠物品失败");
        PageHelper.startPage(pageNum, pageSize);
        List<Donation> donationList = backDonationMapper.getDonationArrival(query);
        PageInfo<Donation> donationPageInfo = new PageInfo<>(donationList);
        List<DonationVo> donationVoList = new ArrayList<>();
        Donation donation = null;
        for(int i = 0; i < donationList.size(); i++) {
            DonationVo donationVo = new DonationVo();
            donation = donationList.get(i);
            BeanUtils.copyProperties(donation, donationVo);
            if(!donation.getImageUrl().equals("")) {
                String []imageUrls = donation.getImageUrl().split(";");
                List<String> imageUrlList = new ArrayList<>();
                for(String imageUrl : imageUrls) {
                    imageUrlList.add(imageUrl);
                }
                donationVo.setImageUrlList(imageUrlList);
            }

            donationVo.setDonationState("已送达");
            donationVoList.add(donationVo);
        }
        dataVo.setCode(200);
        dataVo.setMsg("获取捐赠物品成功");
        dataVo.setData(donationVoList);
        dataVo.setCount(donationPageInfo.getTotal());
        return dataVo;
    }
}
